package com.weitu.task.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.weitu.task.entity.Task;
import com.weitu.task.mapper.TaskMapper;
import com.weitu.task.service.TaskService;
import com.weitu.util.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    private TaskMapper taskMapper;

    @Override
    public Task selectById(String id) {
        return this.taskMapper.selectById(id);
    }

    @Override
    public int deleteById(String id) {
        return this.taskMapper.deleteById(id);
    }

    @Override
    public int updateById(Map record) {
        return this.taskMapper.updateById(record);
    }

    @Override
    public int insert(Map record) {
        return this.taskMapper.insert(record);
    }

    @Override
    public List<Task> selectAllTask(Map map) {
        return this.taskMapper.selectAllTask(map);
    }

    @Override
    public int selectTaskCount(Map map) {
        return this.taskMapper.selectTaskCount(map);
    }

    @Override
    @Transactional
    public boolean insertData(Map<String, Object> paramMap) {
        boolean insertFlag=false;
        String dateTime=paramMap.get("addTime").toString();
        String loginUserId=paramMap.get("addUserId").toString();
        String loginUserDeptId=paramMap.get("addDeptId").toString();
        if (paramMap.get("taskTb")!=null){
            String dataStr = paramMap.get("taskTb").toString();
            List<Map> list = JSONArray.parseArray(dataStr,Map.class);
            for(Map m:list){
                String uuid = IDGenerator.getId();
                m.put("id",uuid);
                m.put("delState",0);
                m.put("addTime", dateTime);
                m.put("addUserId", loginUserId);
                m.put("addDeptId", loginUserDeptId);
                m.put("state",1);
            }
            paramMap.put("taskList",list);
            int insertInt=this.taskMapper.insert(paramMap);
            if (insertInt>0){
                insertFlag=true;
            }
        }else {
            insertFlag=true;
        }
        return insertFlag;
    }

    @Override
    public List<Task> selectByTaskId(Map map) {
        return this.taskMapper.selectByTaskId(map);
    }

    @Override
    public int insertSelective(Map<String, Object> map) {
        map.put("state",1);
        map.put("delState",0);
        return this.taskMapper.insertSelective(map);
    }

}